package xin.marcher.module.construction.service.impl;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import xin.marcher.module.construction.converter.FrontCategoryConverter;
import xin.marcher.module.construction.domain.dto.FrontCategoryDTO;
import xin.marcher.module.construction.domain.dto.FrontCategoryResultDTO;
import xin.marcher.module.construction.domain.dto.FrontCategoryTreeDTO;
import xin.marcher.module.construction.domain.entity.FrontCategoryDO;
import xin.marcher.module.construction.domain.request.FrontCategoryRequest;
import xin.marcher.module.construction.domain.request.QueryCategoryRequest;
import xin.marcher.module.construction.repository.FrontCategoryRepository;
import xin.marcher.module.construction.service.FrontCategoryService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 前台类目信息
 */
@Service
public class FrontCategoryServiceImpl implements FrontCategoryService {

    @Resource
    private FrontCategoryRepository frontCategoryRepository;

    @Resource
    private FrontCategoryConverter frontCategoryConverter;

    /**
     * 查询前台类目
     *
     * @param queryCategoryRequest
     * @return
     */
    @Override
    public List<FrontCategoryTreeDTO> selectTree(QueryCategoryRequest queryCategoryRequest) {
        return frontCategoryConverter.converterTreeList(frontCategoryRepository.selectTree(queryCategoryRequest));
    }

    /**
     * 查询某个层级下的前台类目树(默认不带条件查询父类)
     *
     * @param queryCategoryRequest
     * @return
     */
    @Override
    public List<FrontCategoryDTO> selectChild(QueryCategoryRequest queryCategoryRequest) {
        // 查询某个层级的前台类目树
        List<FrontCategoryDO> categoryInfoList = frontCategoryRepository.listBy(queryCategoryRequest);
        // 返回查询结果
        return frontCategoryConverter.converterList(categoryInfoList);
    }

    /**
     * 新增或者修改前台类目树
     *
     * @param frontCategoryRequest
     */
    @Override
    public FrontCategoryResultDTO saveFrontCategory(@Validated FrontCategoryRequest frontCategoryRequest) {
        // 保存 前台类目树
        frontCategoryRepository.saveOrUpdate(frontCategoryRequest);
        // 返回结果信息
        return new FrontCategoryResultDTO(Boolean.TRUE);
    }

}
